//
//  XUWebViewController.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-11.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//
//  test push
//

#import "XUWebViewController.h"
#import "XUFirstController.h"
#import "AFNetworking/AFNetworking.h"
#import "XUTabBarController.h"
#import "UIWindow+Extension.h"
#import "XUMessegeModel.h"
#import "XUOuthModel.h"

#define OU_CLIENT_ID @"178312144"
#define OU_REDIRECT_URL @"http://xuwenxindeai.blog.163.com/"
#define OU_CLIENT_SECRET @"e224d602833b14f8b13929146d39748e"
#define OU_ACCESS_TOKEN @"https://api.weibo.com/oauth2/access_token"

@interface XUWebViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation XUWebViewController
- (IBAction)close:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url =[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=178312144&response_type=code&redirect_uri=http://xuwenxindeai.blog.163.com/"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];

    
    self.webView.delegate = self;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = request.URL.absoluteString;
    NSLog(@"str======%@",str);
    if ([str containsString:@"?code="]) {
        NSLog(@"判断操作%@",str);
        NSRange rang = [str rangeOfString:@"code="];
        NSUInteger count = rang.location + rang.length;
        NSString *code = [str substringFromIndex:count];
        [self webAccessTokenWithCode:code];
    return NO;
    }
    
    return YES;
    
}

- (void)webAccessTokenWithCode:(NSString *)code
{
    // 请求数据
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"client_id"] = OU_CLIENT_ID;
    parameter[@"client_secret"] = OU_CLIENT_SECRET;
    parameter[@"grant_type"] = @"authorization_code";
    parameter[@"code"] = code;
    parameter[@"redirect_uri"] = OU_REDIRECT_URL;
    // 利用AFN请求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager POST:OU_ACCESS_TOKEN parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"接收到数据%@",responseObject);
        NSDictionary *jsonDict = responseObject;
        XUOuthModel *account = [XUOuthModel outhWithDict:jsonDict];

        
        [account save];
        
        [UIWindow chooseViewController];
        NSLog(@"授权成功....");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"加载失败%@",error);
    }];

}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"将要加载");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载完成");

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"加载失败");

}

- (void)dealloc
{
    
    NSLog(@"webViewDealloc");
}

@end
